#import "UmengLinkFlutterPlugin.h"
#import <UMLink/UMLink.h>

@interface UmengLinkFlutterPlugin () <MobClickLinkDelegate>
@end


@implementation UmengLinkFlutterPlugin{
    bool  isInit;
    FlutterEventSink _eventSink;
    
    NSString *_path;
    NSString *_uri;
    NSDictionary *linkParams;
    NSDictionary *installParams;
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"umeng_link_flutter"
            binaryMessenger:[registrar messenger]];
    
  UmengLinkFlutterPlugin* instance = [[UmengLinkFlutterPlugin alloc] init];

  [registrar addMethodCallDelegate:instance channel:channel];
    
  [registrar addApplicationDelegate:instance];
    
    
    FlutterEventChannel*  eventChannel =
              [FlutterEventChannel eventChannelWithName:@"umeng_link_flutter_event"
                                        binaryMessenger:[registrar messenger]];
  
    [eventChannel setStreamHandler:instance];
    instance->isInit = false;
}

- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)eventSink {
  _eventSink = eventSink;
  NSLog(@"FFmpegKitFlutterPlugin %p started listening to events on %p.\n", self, eventSink);
  
  return nil;
}

- (FlutterError *)onCancelWithArguments:(id)arguments {
  _eventSink = nil;
  return nil;
}

//Universal link的回调
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *_Nonnull))restorationHandler {
    return [MobClickLink handleUniversalLink:userActivity delegate:self];
}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [MobClickLink handleLinkURL:url delegate:self];
}
//URL Scheme回调，iOS9以上，走这个方法
- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
    return [MobClickLink handleLinkURL:url delegate:self];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  } else if([@"init" isEqualToString:call.method])
  {
      if(self->isInit) {
          result(nil);
          return;
      }
      self->isInit = true;
      
      [MobClickLink getInstallParams:^(NSDictionary *params, NSURL *URL, NSError *error) {
            [self invokeMethodInstall:params :URL :error];
      }
    enablePasteboard:NO];
        result(nil);
       }
  
  else {
    result(FlutterMethodNotImplemented);
  }
}


- (void)getLinkPath:(NSString *)path params:(NSDictionary *)params {
    _path = path;
    linkParams = params;
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
                                                       options:0
                                                         error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    self->_eventSink(jsonString);
}

- (void)invokeMethodInstall:(NSDictionary *)params :(NSURL *)url :(NSError *)error {
    NSError *error2 = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
                                                       options:0
                                                         error:&error2];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    self->_eventSink(jsonString);
}

@end
